// pages/order/index.js
import api from '../../request/api.js'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav:['全部','进行中','完成'],
    active:1,
    pageNum:1,
    orderList: [],
    path:''
  },
  //去订单详情
  goDetail(e){
    
    let id = e.currentTarget.dataset.id
    console.error(e)
    wx.navigateTo({
      url: '../orderDetail/index?id='+id,
    })
  },

  toggleTab(e){
    console.log(e);
    this.setData({
      active: e.currentTarget.dataset.index
    })
    const type = e.currentTarget.dataset.index
    this.getOrderList(type)
  },
  async getOrderList(type){
    const pageNum = 1
    const { data: res } = await api('GET', 'Recycle/order/getMyOrderList',{
      token: wx.getStorageSync('token'),
      pageNum,
      flag: type
    })
    if(res.code ===200){
      let newOrderList = this.listArray(res.data)
      this.setData({
        orderList: newOrderList
      })
    }
  },
  async getMoreOrderList(page) {
    const { pageNum, active, orderList } = this.data
    const { data: res } = await api('GET', 'Recycle/order/getMyOrderList', {
      token: wx.getStorageSync('token'),
      pageNum: page,
      flag: active
    })
    if (res.code === 200) {
      if(res.data.length<10){
        Toast('没有更多了')
      }else{
        let newOrderList = orderList.concat(this.listArray(res.data))
        this.setData({
          orderList: newOrderList
        })
      }
    }
  },
    // 图片数据处理
    listArray(data){
      for (let i = 0; i < data.length; i++) {
        switch(data[i]['category_name']){
          case "废纸":
            data[i]['image'] = "../../images/paper.png";
          break;
          case "餐厨":
            data[i]['image'] = "../../images/canchu.png";
          break;
          case "金属":
            data[i]['image'] = "../../images/jinshu.png";
          break;
          case "suliao":
            data[i]['image'] = "../../images/suliao.png";
          break;
          default:
            data[i]['image'] = "../../images/plastic.png";
            break;
        }
      };
      return data;  
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let pages = getCurrentPages(); //页面对象
    let prevpage = pages[pages.length - 2]; //上一个页面对象
    var path = prevpage.route;
    this.setData({
      path: path
    }),
    this.getOrderList(1)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    if(this.data.path != 'pages/my/index'){
      wx.switchTab({
        url: '../index/index',
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { pageNum } = this.data
    console.log(pageNum+1)
    let a = pageNum + 1
    this.getMoreOrderList(a)
    this.setData({
      pageNum: pageNum + 1
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})